// @ts-nocheck
/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { TurboModule } from '@rnoh/react-native-openharmony/ts';
import { display } from '@kit.ArkUI';
import { FoldStatus } from './core/FoldConfig';
import { FolderStackModuleSpec } from './FolderStackModuleSpec';
import { TurboModuleContext } from '@rnoh/react-native-openharmony';

export class FolderStackModule extends TurboModule implements FolderStackModuleSpec.Spec {
  protected ctx: TurboModuleContext;

  constructor(ctx: TurboModuleContext) {
    super(ctx);
    this.ctx = ctx;
  }

  getFoldCreaseRegion(): number[][] {
    if (!display.isFoldable()) {
      return [];
    }
    const creaseRegions: number[][] = [];
    display
      .getCurrentFoldCreaseRegion()
      .creaseRects.forEach((rect: display.Rect) => {
        // Height of the avoidance area in the upper half screen and height of the avoidance area.
        creaseRegions.push([
          px2vp(rect.left),
          px2vp(rect.top),
          px2vp(rect.width),
          px2vp(rect.height),
        ]);
      });
    return creaseRegions;
  }

  isFoldable(): boolean {
    return display.isFoldable();
  }

  getFoldStatus(): FoldStatus {
    if (!display.isFoldable()) {
      return 'unknown';
    }
    switch (display.getFoldStatus()) {
      case display.FoldStatus.FOLD_STATUS_UNKNOWN:
        return 'unknown';
      case display.FoldStatus.FOLD_STATUS_EXPANDED:
        return 'expanded';
      case display.FoldStatus.FOLD_STATUS_FOLDED:
        return 'folded';
      case display.FoldStatus.FOLD_STATUS_HALF_FOLDED:
        return 'half-folded';
      // 三折叠区域
      case display.FoldStatus.FOLD_STATUS_EXPANDED_WITH_SECOND_EXPANDED:
        return 'expanded';
      case display.FoldStatus.FOLD_STATUS_EXPANDED_WITH_SECOND_HALF_FOLDED:
        return 'expanded-with-second-half-folded';
      case display.FoldStatus.FOLD_STATUS_FOLDED_WITH_SECOND_EXPANDED:
        return 'folded-with-second-expanded';
      case display.FoldStatus.FOLD_STATUS_FOLDED_WITH_SECOND_HALF_FOLDED:
        return 'folded-with-second-half-folded';
      case display.FoldStatus.FOLD_STATUS_HALF_FOLDED_WITH_SECOND_EXPANDED:
        return 'half-folded-with-second-expanded';
      case display.FoldStatus.FOLD_STATUS_HALF_FOLDED_WITH_SECOND_HALF_FOLDED:
        return 'half-folded-with-second-half-folded';
    }
    return 'unknown';
  }

  addFoldListener(): void {
    display.on('foldStatusChange', () => {
      this.ctx.rnInstance.emitDeviceEvent(
        'foldStatusChange',
        this.getFoldStatus()
      );
    });
  }

  removeFoldListener(): void {
    display.off('foldStatusChange');
  }
}
